package se.webbzon.oschi01.item;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.Map.Entry;

import se.webbzon.oschi01.json.JsonArray;
import se.webbzon.oschi01.json.JsonNumber;
import se.webbzon.oschi01.json.JsonObject;
import se.webbzon.oschi01.json.JsonString;

public class TranslationJsonWriter implements TranslationWriter {

	@Override public void write(Map<ItemEntry, ItemEntry> data, OutputStream destination) throws IOException {
		JsonArray array = new JsonArray();
		for (Entry<ItemEntry,ItemEntry> entry : data.entrySet()) {
			JsonObject obj = new JsonObject();
			JsonObject from = new JsonObject();
			from.put(new JsonString("id"), new JsonNumber(entry.getKey().getId()));
			from.put(new JsonString("type"), new JsonNumber(entry.getKey().getType().ordinal()));
			from.put(new JsonString("mod"), new JsonString(entry.getKey().getMod()));
			from.put(new JsonString("name"), new JsonString(entry.getKey().getUnlocalisedName()));
			from.put(new JsonString("class"), new JsonString(entry.getKey().getClassName()));
			obj.put(new JsonString("from"), from);
			JsonObject to = new JsonObject();
			to.put(new JsonString("id"), new JsonNumber(entry.getValue().getId()));
			to.put(new JsonString("type"), new JsonNumber(entry.getValue().getType().ordinal()));
			to.put(new JsonString("mod"), new JsonString(entry.getValue().getMod()));
			to.put(new JsonString("name"), new JsonString(entry.getValue().getUnlocalisedName()));
			to.put(new JsonString("class"), new JsonString(entry.getValue().getClassName()));
			obj.put(new JsonString("to"), to);
			array.add(obj);
		}
		PrintStream ps = new PrintStream(destination);
		array.printTo(ps);
	}

}
